/**
 * 
 */
package managesystem;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import controlsystem.Course;
import controlsystem.CourseDescription;
import controlsystem.CourseManager;

/**
 * @author h4oxer
 *
 */
public class addStudCourseServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp){
		HttpSession session = req.getSession();
		Student currStud = (Student) session.getAttribute("currStud");
		String choice = (String) req.getParameter("courseChoice");
		
		CourseDescription c = CourseManager.findByName(choice);
		Course courseobj = CourseManager.createCourseObj(c.getId());
		currStud.addCourse(courseobj);
		StudentManager.add(currStud); //objectify voert een update uit als het object al een ID heeft dus adden en updaten is in se hetzelfde.
		session.setAttribute("currStud",currStud); // update the current student-object with the new course.
		String msg = "Course added succesfully. You can start tracking activity for this course now.";
		session.setAttribute("message", msg);
		
		try {
			resp.sendRedirect("/index.jsp?id=ucp");
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp){
		doPost(req,resp);
	}
}
